package com.roy.eureka.client.controller;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.roy.eureka.api.dto.LoginDTO;
import com.roy.eureka.api.message.RequestMessage;
import com.roy.eureka.api.service.SwordRiskService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
@RequestMapping("SwordRisk")
public class SwordRiskController {

	@Resource
	private SwordRiskService service;
	@Resource
	private RestTemplate restTemplate;
	
	private ThreadLocal<Long> tl = new ThreadLocal<Long>();
	
	@ApiOperation(value = "SwordRisk loginChk接口", notes = "实时风控登录业务接口，feign方式调用")
	@ApiParam(value="loginDto",name="登录接口参数")
	@RequestMapping(value = "/loginChk", method = {RequestMethod.POST})
	@ResponseBody
	public RequestMessage loginChk(LoginDTO loginDto){
		System.out.println("request loginChk loginDto:"+JSON.toJSONString(loginDto));
		tl.set((new Date()).getTime());
		RequestMessage swordRiskInfo = service.loginChk(loginDto);
		System.out.println("res = "+swordRiskInfo);
		System.out.println("duration = "+ ((new Date()).getTime() - tl.get()) + "毫秒");
		return swordRiskInfo;
	}
	
	@ApiOperation(value = "SwordRisk loginChk接口", notes = "实时风控登录业务接口，ribbon方式调用")
	@ApiParam(value="loginDto",name="登录接口参数")
	@RequestMapping(value = "/loginChkByRibbon", method = {RequestMethod.POST})
	@ResponseBody
	public RequestMessage loginChkByRibbon(LoginDTO loginDto){
		System.out.println("request loginChk loginDto:"+JSON.toJSONString(loginDto));
		RequestMessage swordRiskInfo = new RequestMessage();
		try{
			tl.set((new Date()).getTime());
			String requestUrl = "http://EurekaServerDemo/SwordRisk/loginChk";
			swordRiskInfo = restTemplate.postForObject(requestUrl, loginDto, RequestMessage.class);
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println("res = "+swordRiskInfo);
		System.out.println("duration = "+ ((new Date()).getTime() - tl.get()) + "毫秒");
		return swordRiskInfo;
	}
}
